import BuyAPI from '../repo/api/BuyAPI'

export const ZY_APP_ID = ''; //授权登录，开发者帐号appid
export const ZY_API = 'https://api.zhiyecn.com/';//后台地址

/**
 * 获取微信Code
 *
 */
export const getWxCode = () => {
  let url = window.location.href;
  url = url.replace('://', '_')
  url = url.replace(/\//g, '_')
  url = url.replace(/\?/g, '~')

  let redirectUrl = ZY_API + url + '/wei/authorizeRedirect.hx';
  let href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + ZY_APP_ID + '&redirect_uri=' + redirectUrl + '&response_type=code&scope=snsapi_userinfo&state=fcty' + '#wechat_redirect';

  window.location.href = href
};

/**
 * @param code： 微信code
 * @param callBack: 回调
 * 通过code获取 微信用户信息信息。
 * */
export const getWxInfoByCode = (code, callBack) => {
  BuyAPI.getOpenId(code).then((result) => {
    let data = result.responseData;
    callBack(data)
  }).catch(e => {
    console.error(e)
  })
};

/**
 * 微信pc端登录
 * */
export const getWxPcUnionId = (code, callBack) => {
  BuyAPI.getPcUnionId(code).then((result) => {
    let data = result.responseData;
    callBack(data)
  }).catch(e => {
    console.error(e)
  })

};







